//
//  HomeController.swift
//  WeiBo001
//
//  Created by ZhiQiang Li on 16/7/19.
//  Copyright © 2016年 ZhiQiang Li. All rights reserved.
//

import UIKit

private let homeCellId = "homeCellId"

class HomeController: VisitorController {
    
    // 全局的模型数组
    lazy var statusListViewModel: CZStatusListViewModel = CZStatusListViewModel()
    
 
    
    override func viewDidLoad() {
        super.viewDidLoad()

    if !isLogin {
   visitorV?.setupVisitorViewInfo(nil, title: nil)
    return
    }
    setupTableViewInfo()
    getHomeData()
}
    
// 设置tableView
func setupTableViewInfo(){
    navigationController!.view.insertSubview(pullDownTipLabel, belowSubview: navigationController!.navigationBar)
    
    
    
    // 设置数据源代理
    tableView.dataSource = self
    tableView.delegate = self
    // 注册cell
    tableView.registerClass(SHStautsCell.self, forCellReuseIdentifier: homeCellId)
    // 设置tableViewCell高度
    tableView.estimatedRowHeight = 200
     tableView.separatorStyle = .None
    tableView.tableFooterView = indicatorView
    tableView.addSubview(refreshControl)
    // 添加事件
    refreshControl.addTarget(self, action: "refreshAction", forControlEvents: UIControlEvents.ValueChanged)
    
}
    func refreshAction(){
        getHomeData()
    }

    //加载菊花
    
    private var indicatorView:UIActivityIndicatorView = {
      let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
          view.color = ThemeColor
        return view
    }()
    // 系统下载刷新
    private lazy var refreshControl: SHRefreshControl = SHRefreshControl()
    //请求微博显示
    private lazy var pullDownTipLabel: UILabel = {
        let lab = UILabel(textColor: UIColor.whiteColor(), textFont: 12)
        lab.frame = CGRect(x: 0, y: 64 - 30, width: SCREENW, height: 30)
        lab.textAlignment = .Center
        lab.backgroundColor = ThemeColor
        lab.hidden = true
        return lab
    }()

}

//  - UITableViewDataSource
extension HomeController: UITableViewDataSource,UITableViewDelegate {
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListViewModel.staList.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(homeCellId, forIndexPath: indexPath) as! SHStautsCell
        cell.statusViewModel = statusListViewModel.staList[indexPath.row]
        return cell
    }
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        
        // 如果判断当前显示的是最后一个cell 的时候 才可以叫菊花转 且 当前菊花没有开始动画
        if indexPath.row == statusListViewModel.staList.count - 1 && !indicatorView.isAnimating(){
            // 开始动画
            indicatorView.startAnimating()
           getHomeData()
        }
    }
    
}



extension  HomeController {
    func getHomeData(){
        
        statusListViewModel.loadHomeData(indicatorView.isAnimating()) { (isSuc,count) -> () in
            // 停止动画
            self.endRefreshing()
            if !isSuc {
                print("请求数据失败")
                return
            }
            self.tableView.reloadData()
            if count >= 0{
                self.setpullDownTipLabelAnim(count)
            }
        }
        
    }
    // 设置微博提示动画
    func setpullDownTipLabelAnim(count: Int){
        // 判断如果当前在动画也就是显示 就不在进行动画
        if pullDownTipLabel.hidden == false{
            return
        }
        // 显示空间
        pullDownTipLabel.hidden = false
        var name = ""
        if count == 0 {
            name = "您已经是最新的微博数据啦"
        }else {
            name = "您更新了\(count)条微博"
        }
        // 设置text
        pullDownTipLabel.text = name
        
        // 设置动画
        UIView.animateWithDuration(0.5, animations: { () -> Void in
            
            self.pullDownTipLabel.transform = CGAffineTransformMakeTranslation(0, 30)
            
            }) { (_) -> Void in
                UIView.animateWithDuration(0.5, delay: 1, options: [], animations: { () -> Void in
                    self.pullDownTipLabel.transform = CGAffineTransformIdentity
                    }, completion: { (_) -> Void in
                        // 完成后隐藏
                        self.pullDownTipLabel.hidden = true
                })
        }
    }
    
    
    // 停止各种动画
    func endRefreshing(){
        self.indicatorView.stopAnimating()
        self.refreshControl.endRefreshing()
    }
}



extension HomeController {
    func test(){
}
}
